/**
 * Created with JetBrains WebStorm.
 * User: sparks345
 * Date: 12-6-9
 * Time: 下午10:31
 * To change this template use File | Settings | File Templates.
 */


QZONE = window.QZONE || {};

QZONE.Finder = QZONE.Finder || {};

QZONE.Finder.ItemHit = function() {
	var str2dom = function(str) {
		str2dom._div.innerHTML = str;
		return str2dom._div.children.length>1
				? str2dom._div.getElementsByTagName("*")
				: str2dom._div.firstChild;
	};
	str2dom._div = document.createElement("div");

	function extend(q,p){var r=function(){};r.prototype=p.prototype;q.prototype=new r();q.constructor=q;return q}

	var Hit = function(dt) {
		this.data = dt;
		this.dom=str2dom(this._border);
		this.init();
	};

	Hit.prototype = {
		_border : [
			'<p class="item_hint"></p>'
		].join(""),
		_template : "",
		setData : function(dt){
			this.data = dt;
		},
		init : function() {
			var ctt = str2dom(this.genPanel());
			this.dom.appendChild(ctt);
		},
		genPanel : function() {
			return QZFL.string.format(this._template, this.data);
		},
		getDom : function() {
			return this.dom;
		},
		bind2Dom : function(dom) {//debugger
			dom.appendChild(this.getDom());
			//adjust position done by css...
			var rect = QZFL.dom.getRect(this.getDom()), padding=20;
			var oCotRect = QZFL.dom.getRect($("_finder_container"));
			var oWidth = oCotRect.left + oCotRect.width;
			var oBottom = oCotRect.bottom;
			var iii = document.createElement("i");
			this.getDom().appendChild(iii);
//			var iii = $e("i", this.getDom()).eq(0);
			if(rect.left + rect.width + padding > oWidth) {
				iii.className = "arrow_right";
				this.getDom().style.right = "85px"; //"-240px";
			}else{
				iii.className = "arrow_left";
				this.getDom().style.left = "85px";
			}
			if(rect.bottom + padding > oBottom && rect.height < oCotRect.height - 100) {
				iii.className += " bybtm";
				this.getDom().style.bottom = "20px";//"-50px";
			}else{
				this.getDom().style.top = "5px";
			}
		},
		close : function(dom, cb) {
			this.getDom().parentNode.removeChild(this.getDom());
			cb && cb();
		}
	};

	Hit._instance = null;
		Hit.close = function(dom, cb) {
			Hit._instance && Hit._instance.close(dom, cb);
			Hit._instance = null;
//			cb && cb();
		};
		Hit.parse = function(dom) {//debugger
			Hit.close(dom);
			var id = dom.getAttribute("data-id");
	//		var data = 'xxx';

	//		setTimeout(function(data){
			var data = ItemFactory.getDataById(id);
			if(data){//debugger
				switch (data.type) {
					case 0://日志
						Hit._instance = new NoteHit(data);
						break;
					case 2://说说
						Hit._instance = new ShuoshuoHit(data);
						break;
					case 3://音乐
						Hit._instance = dom.getAttribute("playing") ? MusicHit._instance : /*MusicHit._instance ||*/ new MusicHit(data);
						break;
					case 1://图片
					case 4://相册
						Hit._instance = new AlbumHit(data);
						break;
				}
			}

			Hit._instance && Hit._instance.bind2Dom(dom);
	//		},200);
		};

	//////////////////////
	var NoteHit = extend(function(){
		Hit.apply(this, arguments);
	}, Hit);
	NoteHit.prototype._template = '<span class="text"><b><a href="javascript:QZONE.FP.toApp(\'{link}\');">{title}</a></b><br/>{summary}</span>';

	var AlbumHit = extend(function(){
		Hit.apply(this, arguments);
	}, Hit);
	AlbumHit.prototype._template = '<span class="img"><a href="javascript:QZONE.FP.toApp(\'{link}\');"><img src="{img}"></a></span>';

	var MusicHit = extend(function(){
		Hit.apply(this, arguments);
	}, Hit);
	MusicHit.prototype._border = '<p class="item_hint_music"></p>';
	MusicHit.prototype._template = '<span title="点击播放音乐"><i class="play"></i></span>';///* onclick="this.className='pause'*/
	MusicHit.prototype.bind2Dom = function(dom) {//debugger
		if($e("i", dom).eq(0)) return;

		var me = this;
		dom.appendChild(this.getDom())

		//绑定播放音乐事件
		var btn = $e("i", this.getDom());
		btn.onClick(function(){//debugger
			var tgt = QZFL.event.getTarget();
			if(tgt.className=="play") {
				tgt.className="pause";
//				MusicHit.swapPlay(me);
				me.getDom().parentNode.setAttribute("playing", "...");

				MusicHit._instance && MusicHit._instance.getDom().parentNode && MusicHit._instance.getDom().parentNode.removeAttribute("playing");
				MusicHit._instance && MusicHit._instance.dispose();
				MusicHit._instance = me;

				var sid = me.data.sid;console.log("::::music play : sid->"+sid);
				MusicHit.player.play(me.data);
			}else{
				tgt.className="play";
				me.getDom().parentNode.removeAttribute("playing");
				MusicHit._instance && MusicHit._instance.dispose();
				var sid = me.data.sid;console.log("::::music pause");
				MusicHit.player.pause();
			}
		});


	};
	MusicHit.prototype.close = function(dom){
		if(dom.getAttribute("playing")) {

		}else{
			this.dispose();
		}
//		var ply = $e("i", this.getDom()).eq(0);
//		if(ply){
//			if(ply.className=="play"){
//				this.dispose();
//			}else{
//
//			}
//		}
	};
	MusicHit.prototype.dispose = function() {
		this.getDom().parentNode &&
		this.getDom().parentNode.removeChild(this.getDom());
	};
//	MusicHit.swapPlay = function(_new) {
//		MusicHit._instance && MusicHit._instance.dispose();
////			MusicHit._instance && (MusicHit._instance.dispose());
//		_new.getDom().parentNode.setAttribute("playing", "...");
//		MusicHit._instance = _new;
//	};

	MusicHit.player = function() {
		var cur_playone_songid;

		return {
			play:function(dt) {
				var sid = dt.sid;
				if (cur_playone_songid != sid || !QZONE.music.isPlaying()) {
					cur_playone_songid = sid;//name, url, qusid, ifexp, qzonekey, singerName, iSongId, iSongType
					QZONE.music.playSongWithNoCheck(dt.title, dt.url, dt.sid, "", "", dt.singer, dt.sid, dt.stype);
//					QZONE.music.playMusic();
				}
			},
			pause : function() {
				QZONE.music.pauseMusic();
			}
		}
	}();

	var ShuoshuoHit = extend(function(){
		Hit.apply(this, arguments);
	}, Hit);
	ShuoshuoHit.prototype._template = '<span class="text">{title}<br/>{summary}</span>';





	return {
		/**
		 * 绑定事件监听
		 * @param dom
		 */
		init : function() {
			var me = this;

			var cot = $e('.popup_qzonefinder_cont').eq(0);
			QZFL.event.delegate(cot, ".item", "mouseenter", function(){//debugger
//				alert(this);
				me.show(this);
			});
			QZFL.event.delegate(cot, ".item", "mouseleave", function(){
//				alert(this);
				me.hide(this);
			});

			QZONE.Finder.ItemHit.init = QZFL.emptyFn;
		},
		show : function(dom) {
			QZFL.css.addClassName(dom, "item_over");
			var hit = Hit.parse(dom);
//			hit.bind2Dom(dom);
		},
		hide : function(dom) {
			Hit.close(dom, function(){
				QZFL.css.removeClassName(dom, "item_over");
			});
//			QZFL.css.removeClassName(dom, "item_over");
		}
	}
}();

QZONE.Finder.ItemHit.init();